package weixinkeji.dev.dc.base.refectory.h.fl;

import cache.service.IMYCache;
import java.util.List;
import sys.wx.c.ATR;
import sys.wx.c.components.cv.CV;
import sys.wx.ann.hm.H;
import sys.wx.ann.hm.M;
import sys.wx.c.components.tree.TreeService;
import sys.wx.m.db.SQL.mysql.common.where.Where;
import sys.wx.m.db.dao.mysql.Dao;
import sys.wx.util.ToolService;
import weixinkeji.dev.dc.base.refectory.bean.Refectory;
import weixinkeji.dev.dc.base.refectory.bean.fl.RefectoryFL;
import weixinkeji.dev.dc.base.refectory.cache.RefectoryFLCache;

/**
 *
 * @author IK
 */
@H("weixinkeji/dev/dc/base/refectoryfl/adu")
public class RefectoryFLADU {

    ATR a;                  //ATR是必须要的。它封装了web请求的相关信息

    public RefectoryFLADU(ATR obj) {     //这个有参构造函数，也是必须的。它进行附值
        this.a = obj;
    }

    @M("/add")
    public String a1() {
        RefectoryFL obj = CV.getObject_Add(RefectoryFL.class, a.request, new weixinkeji.dev.dc.base.refectory.check.CheckRefectoryFL().doRefectorFL(false));
        if (null == obj) {
            a.out_OK_NO(false, "您提交的数据存在格式问题。请注意数据字符的输入");
            return null;
        }
        a.out_OK_NO(Dao.add(obj));
        IMYCache.remove(RefectoryFLCache.KEY);
        return null;
    }

    @M("/dell")
    public String d() {
        String id = a.request.getParameter("id");
        if (id.length() < 26) {
            return null;
        } else if (Dao.selectCount(Refectory.class, Where.putIN(Where.TJ_IGNORE_NOTHING, Refectory.class, "", "flid", CV.toSQLStringValue(id))) > 0) {
            a.out_OK_NO(false, "此分类已经投入使用，无法直接删除");
            return null;
        }
        a.out_OK_NO(Dao.dell_vast(RefectoryFL.class, id));
        IMYCache.remove(RefectoryFLCache.KEY);
        return null;
    }

    @M("/update")
    public String u() {
        return null != a.request.getParameter("id") ? select1(a.request.getParameter("id")) : update();
    }

    private String select1(String x) {
        RefectoryFL obj = Dao.selectOneByID(RefectoryFL.class, x);
        a.request.setAttribute("RefectoryFL", obj);
        a.request.setAttribute("RefectoryFL_P", Dao.selectOneByTJ(RefectoryFL.class, Where.putThenGet(Where.TJ_IGNORE_NOTHING, RefectoryFL.class, "", "flid", "=", obj.getFlpid())));
        return "/dc_manager/base/refectory/fl/refectoryfl_U.jsp";
    }

    private String update() {
        RefectoryFL obj = CV.getObject(RefectoryFL.class, a.request, new weixinkeji.dev.dc.base.refectory.check.CheckRefectoryFL().doRefectorFL(true));
        if (null == obj) {
            a.out_OK_NO(false, "您提交的数据存在格式问题。请注意数据字符的输入");
            return null;
        } else if (ToolService.IsNullOREmpty(obj.getFlpid())) {
            obj.setFlpid("0");
        }
        String errorMsg;
        errorMsg = TreeService.getError_FatherIsSon((List) IMYCache.get(RefectoryFLCache.KEY), "flid", "flpid", obj.getFlid(), obj.getFlpid());
        if (null != errorMsg) {
            a.out_OK_NO(false, errorMsg);
            return null;
        }
        a.out_OK_NO(Dao.update(obj));
        IMYCache.remove(RefectoryFLCache.KEY);
        return null;
    }
}
